package com.hj.controller;



import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.jws.WebParam.Mode;
import javax.servlet.http.HttpSession;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hj.Tools.MD5;
import com.hj.domain.Studentinfo;
import com.hj.exception.CustomException;
import com.hj.service.inter.StudentServiceInter;
//对数据库studentinfo表的操作
//与业务功能无关的异常建议在controller抛出异常
@Controller
@RequestMapping(value="/Student")
public class StudentController {
	
	@Autowired
	private StudentServiceInter studentServiceInter;
	
	//用户注册
	@RequestMapping("/insertUser")
	public String insertUser(String name,String nickname,String birthday,
			String sex,String password,String phone,String mail,String address,String hobby,String image,String registerDate,String lastloginTime )throws Exception{
		//获取系统当前时间
		Date d = new Date();   
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String dateNowStr = sdf.format(d);  
        
		Studentinfo studentinfo=new Studentinfo();
		studentinfo.setStuid(UUID.randomUUID().toString());
		studentinfo.setName(name);
		studentinfo.setNickname(nickname);
		studentinfo.setBirthday(birthday);
		studentinfo.setSex(sex);
		studentinfo.setPassword(MD5.toMD5(password));
		studentinfo.setPhone(phone);
		studentinfo.setMail(mail);
		studentinfo.setAddress(address);
		studentinfo.setHobby(hobby);
		studentinfo.setImage(image);
		studentinfo.setRegisterdate(dateNowStr);
		studentinfo.setLastlogintime(dateNowStr);
		studentServiceInter.insertUser(studentinfo);
		//注册成功，返回登录页面
		return "";
		
	}
	//更新用户
	@RequestMapping(value="/updatetUser",method=RequestMethod.POST)
	public String updatetUser(String stuId,String name,String nickname,String birthday,
			String sex,String password,String phone,String mail,String address,String hobby,String image,Model model)throws Exception{
		 
		HashMap<String, Object> studentMap = new HashMap<String, Object>();
		studentMap.put("stuId", stuId);
		studentMap.put("name", name);
		studentMap.put("nickname", nickname);
		studentMap.put("birthday", birthday);
		studentMap.put("sex", sex);
		studentMap.put("password", MD5.toMD5(password));
		studentMap.put("phone", phone);
		studentMap.put("mail", mail);
		studentMap.put("address", address);
		studentMap.put("hobby", hobby);
		studentMap.put("image", image);
		studentServiceInter.updateUser(studentMap);
		//信息修改成功
		return "";		
	}
	//查询所有用户信息
	@RequestMapping("/queryAllUser")
	public String queryAllUser(Model model)throws Exception{
		 List<Studentinfo> list=studentServiceInter.queryAllUser();
		 model.addAttribute("allUsers", list);
		 //查询成功
		 return "";
	}
	
}
